<?php

  /**
   * This class implement all methods and properties on a folder.<br />
   * Store the folder name in private private variable for further use.<br />
   *
   * @author Dany Mottier
   * @version 3
   */
  class Folder extends Base implements IFolder {

    /** @var string The name of the directory to work with. */
    private $directory = "";

    /**
     * The collection of files contained in the working directory.
     * @var IArrayList
     */
    public $Files;

    /**
     * The collection of folders contained in the working directory.
     * @var IArrayList
     */
    public $Folders;

    /**
     * Store the folder name in private private variable for further use.<br />
     *
     * @param string $directory The directory to point to.
     *
     * @link http://be.php.net/manual/en/function.is-string.php
     * @link http://be.php.net/manual/en/function.is-dir.php
     */
    public function __construct($directory) {
      if (is_string($directory))
        if (is_dir($directory)) {
          $this->directory = $directory;
        } else {
          throw new IOException("\$directory parameter is not a directory");
        }
      else
        throw new IllegalArgumentException("\$directory parameter should be a string");
    }

    public function getName() {
      return $this->directory;
    }

    public function getCurrent() {
      return getcwd();
    }

    public function change($directory) {
      if (is_string($directory)) {
        if (is_dir($directory)) {
          return chdir($directory);
        } else {
          throw new IOException("\$directory parameter is not a directory");
        }
      } else {
        throw new IllegalArgumentException("\$directory parameter should be a string");
      }
    }

    public function changeRoot($directory) {
      if (function_exists("chroot")) {
        if (is_string($directory)) {
          if (is_dir($directory)) {
            return chroot($directory);
          } else {
            throw new IOException("\$directory parameter is not a directory");
          }
        } else {
          throw new IllegalArgumentException("\$directory parameter should be a string");
        }
      } else {
        throw new BeObjectException("The function 'chroot' is not available in this environment.");
      }
    }

    public function open() {
      return!is_null(opendir($this->directory));
    }

    public function read() {
      return readdir();
    }

    public function rewind() {
      rewinddir();
    }

    public function scan() {
      $this->Files = new ArrayList(array());
      $this->Folders = new ArrayList(array());

      while ($file = $this->Read()) {
        if (is_file($this->directory . $file)) {
          $this->Files->add(new File($this->directory . $file));
        } else {
          $this->Folders->add(new Folder($this->directory . $file));
        }
      }
    }

    public function close() {
      closedir();
    }

    public function getFilesLength() {
      return $this->Files->getLength();
    }

    public function getFoldersLength() {
      return $this->Folders->getLength();
    }

  }

?>
